﻿using System.Windows.Forms;
using System.Xml;
using UIShell.OSGi;

namespace Pasys.PresentationCore.WinForms
{
    public class MainMenuBuilder : BuilderBase<MenuCreator>
    {
        public override void Build(XmlNode xmlNode, IBundle owner)
        {
            ToolbarCreateOptions opts;
            ToolStripItem newItem = ToolbarItemBuilder.Build(xmlNode, owner, out opts);
            var parentItems = Items.FindAll(m => m.Item.Name.Equals(newItem.Name));
            if (parentItems.Count > 0)
            {
                var parentItem = parentItems[0].Item as ToolStripDropDownItem;
                var children = newItem as ToolStripDropDownItem;
                switch (opts.CreateType)
                {
                    case MenuCreateType.Insert:
                        for (int i = children.DropDownItems.Count - 1; i >= 0; i--)
                        {
                            parentItem.DropDownItems.Insert(0, (ToolStripItem)children.DropDownItems[i]);
                        }
                        break;
                    case MenuCreateType.Append:
                    default:
                        while (children.DropDownItems.Count > 0)
                        {
                            parentItem.DropDownItems.Add((ToolStripItem)children.DropDownItems[0]);
                        }
                        //for (int i = 0; i < children.DropDownItems.Count; i++)
                        //{
                        //    parentItem.DropDownItems.Add((ToolStripItem)children.DropDownItems[i]);
                        //}
                        break;
                }
            }
            else
            {
                int idx = -1;
                bool isAfter = false;
                if (!string.IsNullOrEmpty(opts.Before))
                {
                    idx = Items.FindIndex(m => m.Item.Name.Equals(opts.Before));
                }

                if (idx < 0 && !string.IsNullOrEmpty(opts.After))
                {
                    idx = Items.FindIndex(m => m.Item.Name.Equals(opts.After));
                    if (idx >= 0)
                    {
                        isAfter = true;
                    }
                }

                if (idx < 0)
                {
                    idx = Items.FindIndex(m => m.Options.Before.Equals(newItem.Name));
                    if (idx >= 0)
                    {
                        isAfter = true;
                    }
                }
                if (idx < 0)
                {
                    idx = Items.FindIndex(m => m.Options.After.Equals(newItem.Name));
                }

                if (idx >= 0 && isAfter)
                {
                    if (idx + 1 == Items.Count)
                    {
                        idx = -1;
                    }
                    else
                    {
                        idx++;
                    }
                }

                var mc = new MenuCreator(newItem, idx)
                {
                    Options = opts
                };

                if (idx < 0)
                {
                    Items.Add(mc);
                }
                else
                {
                    Items.Insert(mc.Index, mc);
                }
                OnItemAdded(mc);
            }
        }
    }

    public class MenuCreateType
    {
        public const string Insert = "Insert";
        public const string Append = "Append";
    }

    public class MenuCreator
    {
        public ToolStripItem Item { get; private set; }
        public int Index { get; private set; }
        public ToolbarCreateOptions Options { get; set; }

        public MenuCreator(ToolStripItem Item, int Index)
        {
            this.Item = Item;
            this.Index = Index;
        }
    }

}